set scheme lean1
global eventvarlist eventviolent eventshoot eventclash eventkidnap eventbomb eventbombclash eventbck civilian armed

	
import excel "actor.xlsx", firstrow clear
	count
	local N=r(N)
	
	forvalues i=1/`N' {
		local actor`i'=actor[`i']
		local group`i'=group[`i']
		local actor`i'=trim("`actor`i''")
		local group`i'=trim("`group`i''")  
		
		display "`actor`i'' `group`i''"
	}
	
use "bcms_armm", clear
		
	forvalues i=1/7 {
		replace actor`i'=trim(actor`i')
	}
	
	forvalues i=1/6 {
		replace actor_`i'=trim(actor_`i')
	}
	g civilian=0 if missing(actor1)+missing(actor2)+missing(actor3)+missing(actor4)+missing(actor5)+missing(actor6)+missing(actor7)+missing(actor_1)+missing(actor_2)+missing(actor_3)+missing(actor_4)+missing(actor_5)+missing(actor_6)<13
	g armed=0 if missing(actor1)+missing(actor2)+missing(actor3)+missing(actor4)+missing(actor5)+missing(actor6)+missing(actor7)+missing(actor_1)+missing(actor_2)+missing(actor_3)+missing(actor_4)+missing(actor_5)+missing(actor_6)<13
	g government=0 if missing(actor1)+missing(actor2)+missing(actor3)+missing(actor4)+missing(actor5)+missing(actor6)+missing(actor7)+missing(actor_1)+missing(actor_2)+missing(actor_3)+missing(actor_4)+missing(actor_5)+missing(actor_6)<13
	g unknown=0
	forvalues type=1/`N' {
		display "`actor`type'' `group`type''" 
		
		forvalues i=1/7 {
			replace `group`type''=1 if actor`i'=="`actor`type''"
		}
		
		forvalues i=1/6 {
			replace `group`type''=1 if actor_`i'=="`actor`type''"
		}		
	}
	replace unknown=0 if civilian+armed+government>0
	replace civilian=. if unknown==1
	replace armed=. if unknown==1
	replace government=. if unknown==1
	
	*Violent Events
	g eventviolent=0 if (trim(manifest1)=="" &  trim(manifest2)=="" & trim(manifest3)=="" & trim(manifest4)=="")==0
	foreach type in Bombing Shooting Ambuscade Kidnapping/Abduction Clash/Encounter Arson Battery/Assault ///
		Carjacking Robbery/Holdup Theft Stabbing/Hacking Rape Hostage-taking ///
		/*Human_Trafficking*/ Slavery/Servitude Sexual {
		local type=subinstr("`type'","_"," ",.)
		
		forvalues i=1/4 {
			replace eventviolent=1 if trim(manifest`i')=="`type'"
		}
		
	}
	
	*Bombings
	g eventbomb=0 if (trim(manifest1)=="" &  trim(manifest2)=="" & trim(manifest3)=="" & trim(manifest4)=="")==0
	foreach type in Bombing {
		local type=subinstr("`type'","_"," ",.)
		
		forvalues i=1/4 {
			replace eventbomb=1 if trim(manifest`i')=="`type'"
		}
		
	}	
	*Shootings
	g eventshoot=0 if (trim(manifest1)=="" &  trim(manifest2)=="" & trim(manifest3)=="" & trim(manifest4)=="")==0
	foreach type in Shooting {
		local type=subinstr("`type'","_"," ",.)
		
		forvalues i=1/4 {
			replace eventshoot=1 if trim(manifest`i')=="`type'"
		}
	}
	*Clash
	g eventclash=0 if (trim(manifest1)=="" &  trim(manifest2)=="" & trim(manifest3)=="" & trim(manifest4)=="")==0
	foreach type in Clash/Encounter {
		local type=subinstr("`type'","_"," ",.)
		
		forvalues i=1/4 {
			replace eventclash=1 if trim(manifest`i')=="`type'"
		}		
	}	
	
	*Kidnapping
	g eventkidnap=0 if (trim(manifest1)=="" &  trim(manifest2)=="" & trim(manifest3)=="" & trim(manifest4)=="")==0
	foreach type in Kidnapping/Abduction {
		local type=subinstr("`type'","_"," ",.)
		
		forvalues i=1/4 {
			replace eventkidnap=1 if trim(manifest`i')=="`type'"
		}		
	}	
	
	egen eventbombclash=rowmax(eventbomb eventclash) if eventbomb+eventclash<.
	egen eventbck=rowmax(eventbomb eventclash eventkidnap) if eventbomb+eventclash+eventkidnap<.
	g manifest=trim(manifest1)
	g manifestshort=manifest
	replace manifestshort="Kidnapping" if inlist(manifest,"Kidnapping/Abduction","Hostage-taking")
	replace manifestshort="Assault" if inlist(manifest,"Battery/Assault")
	replace manifestshort="Stabbing" if manifest=="Stabbing/Hacking"
	replace manifestshort="Sexual Assault" if inlist(manifest,"Rape","Sexual")
	replace manifestshort="Robbery" if manifest=="Robbery/Holdup"
	replace manifestshort="Clash" if manifest=="Clash/Encounter" 
	
	preserve	
		collapse (sum) eventviolent civilian armed government  if eventviolent==1, by(manifestshort)
		graph bar eventviolent civilian armed /*government*/, ///
		over(manifestshort, label(angle(90))) legend(size(small) position(3) rows(4)  label(1 "Total Events") label(2 "Civilians Involved") ///
			label(3 "Armed Groups Involved") /*label(4 "Government Involved")*/) bar(1, color(navy)) bar(2, color(cranberry) fintensity(60) lwidth(vthin)) bar(4, color(bluishgray) lwidth(vthin) fintensity(80)) bar(3, color(emerald) lwidth(vthin) fintensity(40))
		graph export "bymanifest.png" , as(png) width(4000) replace
	
	restore
	

	preserve
		collapse (sum) eventviolent eventshoot  if eventviolent==1, by(main_category1) 
		graph bar eventviolent eventshoot, over(main_category1, label(angle(90)) ///
			relabel(1 `""Extra-Judicial" "Issues""' 2`""Governance issues" "conflict""' 3`""Identity-based" "conflict""' 4`""Political" "issues""' 5`""Resource-based" "conflict""' 6`""Shadow" "economies""' 7 "Undetermined")) ///
			bar(1, color(navy)) bar(2, color(cranberry) fintensity(60) lwidth(medthick))  legend(size(small) label(1 "Total Events") label(2 "Shootings") ) 
		graph export "bycategory.png" , as(png) width(4000) replace
	restore 


	
	drop humancost* actor* source* weapon* manifest* *categ* role*
	
	reshape long province municipality barangay, i(incidid) j(doublecount)
	drop if province=="" 
		*because province1 is never missing
	bysort incidid: g max=_N
	
	replace province=trim(province)
	replace municipality=trim(municipality)
	replace barangay=trim(barangay)
		
	
	preserve
		use "armmlist", clear
		keep if barancode==0 & provcode!=0
		bysort provcode: egen nmuni=sum(municode!=0)
		keep if municode==0
		tempfile province
		save `province'
	restore
	
	g name=proper(province)
	replace name="City Of Isabela" if name=="Isabela City"

	
	merge m:1 name using `province', keepusing(name provcode nmuni)
		drop if _merge==2
		count if _merge==1 & province!=""
		drop _merge
	
	expand nmuni if municipality==""
	bys incidid province: g n=_n if municipality==""
	 
	
	preserve
		use "armmlist", clear
		keep if barancode==0 & municode!=0
		bys provcode (municode): g n=_n
		rename name municipality
		duplicates drop
		tempfile muniexpand
		save `muniexpand'
	restore

	merge m:1 provcode n using `muniexpand', keepusing(provcode n municipality weight) update
		drop if _merge==2
		
		drop _merge
		

	preserve
		use "armmlist", clear
		replace name=proper(name)
		keep if barancode==0 & municode!=0
		duplicates drop
		tempfile muni
		save `muni'
	restore
	
	replace name=proper(municipality)
	replace name="Bongao (Capital)" if name=="Bongao"
	replace name="Bacolod-Kalawi (Bacolod Grande)" if name=="Bacolod Grande"
	replace name="Panglima Sugala (Balimbing)" if name=="Balimbing (Panglima Sugala)"
	replace name="Mapun (Cagayan De Tawi-Tawi)" if name=="Cagayan De Tawi-Tawi (Mapun)"
	replace name="Datu Blah T. Sinsuat" if name=="Datu Blah Sinsuat"
	replace name="Datu Saudi-Ampatuan" if name=="Datu Saudi Ampatuan"
	replace name="Gen. S. K. Pendatun" if name=="General S.K. Pendatun"
	replace name="City Of Isabela" if name=="Isabela City"
	replace name="Jolo (Capital)" if name=="Jolo"
	replace name="City Of Lamitan" if name=="Lamitan City"
	replace name="Lumba-Bayabao (Maguing)" if name=="Lumba-Bayabao"
	replace name="Marawi City (Capital)" if name=="Marawi City"
	replace name="Madalum" if name=="Mdalum"
	replace name="Lumbatan" if name=="Madalum" & (barangay=="Bubong Macadar"|barangay=="Poblacion (Lumbatan)")
	replace name="Poona Bayabao (Gata)" if name=="Poon-Bayabao (Gata)"
	replace name="Shariff Aguak (Maganoy) (Capital)" if name=="Shariff Aguak"
	replace name="Sultan Sa Barongis (Lambayong)" if name=="Sultan sa Barongis (Lambayong)"
	replace name="Turtle Islands" if name=="Turtle Island"
	replace name="Sitangkai" if name=="Sitangkai (Sibatu)"
	replace name="Sultan Dumalondong" if name=="Sultan Domalondong"
	replace name="Bubong Macadar" if name==""

	merge m:1 provcode name using `muni', keepusing(provcode name municode)
		drop if _merge==2
		count if _merge==1 & municipality!=""
		drop _merge
		*no missing munis because of expand 0 obs master==1
	
	drop name

	bys incidid provcode: g uniqprov=1 if _n==1 
	bys incidid provcode municode: g uniqmun=1 if _n==1
	replace uniqprov=2 if weight<.
	
	replace provcode=99 if provcode==.
	replace municode=99 if municode==.
	
	g provid=string(provcode,"%02.0f")
	g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
	
	replace weight=1 if weight==.
save "bcms_edit", replace

*****Monthly Province Counts
use "bcms_edit", clear
	keep if uniqprov==1|uniqprov==2
	collapse (count) total=incidid (sum) $eventvarlist [iweight=weight] , by(provid year month)
	
	ds year month provid, not
	local varlist = r(varlist)
	  
	foreach var in `varlist' {
		rename `var' `var'_prov
	}


preserve
	keep provid 
	duplicates drop
	expand (2014-2011+1)*12
	bys provid: g year=2011+ceil(_n/12)-1
	bys provid year: g month=_n
	tempfile temp
	save `temp'
restore

merge 1:1 provid year month using `temp'
ds year month provid, not
local varlist=r(varlist)

foreach var in `varlist' {
	replace `var'=0 if _merge==2
}
drop _merge
sort prov year month


save "bcms_byprov", replace
	


*Zero's for the rest of the Philippines and missing months: take list of municipalities/provinces from DHS
use "$dhs2013\phkr61dt\PHKR61FL", clear
	rename sprov provcode
	rename smunic municode
	rename sbarang barancode
	
	keep *code
	g provid=string(provcode,"%02.0f")
	g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
	g baranid=string(provcode,"%02.0f") +string(municode, "%02.0f") +string(barancode, "%03.0f")
	
	keep provid 
	duplicates drop
	expand (2014-2011+1)*12
	bys provid: g year=2011+ceil(_n/12)-1
	bys provid year: g month=_n
	merge 1:1 provid year month  using "bcms_byprov"
	
	ds year month provid _merge, not
	local varlist=r(varlist)
	
	g _inbcms=1 if _merge!=1
	bys provid: egen inbcms=max(_inbcms)
		*second line redundant
		
	
	foreach var in `varlist' {
		replace `var'=0 if _merge==1
	}
	drop _*
	
	foreach var in `varlist' {
		bys provid (year month): g `var'_last2=`var'+`var'[_n-1] 
		bys provid (year month): g `var'_last3=`var'+`var'[_n-1] +`var'[_n-2]
	}
	
	ds *_prov*
	local varlist=r(varlist)
	foreach var in `varlist' {
		bys provid (year month): g la_`var'=`var'[_n-1]
		bys provid (year month): g la2_`var'=`var'[_n-2]
		bys provid (year month): g le_`var'=`var'[_n+1]
		bys provid (year month): g le2_`var'=`var'[_n+2]
		
		
	}		
	g inarmm=inbcms
	replace inarmm=. if substr(provid,1,2)=="35"|substr(provid,1,2)=="47"
	save "bcms_byprov_allph", replace

	   
use "bcms_edit", clear
	keep if uniqprov==1|uniqprov==2
	
	collapse (count) total=incidid (sum) $eventvarlist [iweight=weight] , by(provid  incidentdate)
	
	ds incidentdate provid, not
	local varlist = r(varlist)
	
	foreach var in `varlist' {
		rename `var' `var'_prov
	}


	preserve
		keep provid 
		duplicates drop
		expand (365*4+1)
		bys provid: g incidentdate=mdy(12,31,2010)+_n
		format incidentdate %tdnn/dd/CCYY
		tempfile temp
		save `temp'
	restore
	
	merge 1:1 provid incidentdate using `temp'
	ds incidentdate provid, not
	local varlist=r(varlist)
	
	foreach var in `varlist' {
		replace `var'=0 if _merge==2
	}
	drop _merge

	sort prov  incidentdate

	save "bcms_byprov_date", replace
	
*Zero's for the rest of the Philippines and missing months: take list of municipalities/provinces from DHS
use "$dhs2013\phkr61dt\PHKR61FL", clear
	rename sprov provcode
	rename smunic municode
	rename sbarang barancode
	
	keep *code
	g provid=string(provcode,"%02.0f")
	g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
	g baranid=string(provcode,"%02.0f") +string(municode, "%02.0f") +string(barancode, "%03.0f")
	
	keep provid 
	duplicates drop
	expand (365*4+1)
	bys provid: g incidentdate=mdy(12,31,2010)+_n
	format incidentdate %tdnn/dd/CCYY
	merge 1:1 provid incidentdate using "bcms_byprov_date"
	
	ds incidentdate _merge provid, not
	local varlist=r(varlist)
	
	foreach var in `varlist' {
		replace `var'=0 if _merge==1
	}		
	
	g _inbcms=1 if _merge!=1
	bys provid: egen inbcms=max(_inbcms)
		*second line redundant
	

	drop _*
	
	g inarmm=inbcms
	replace inarmm=. if substr(provid,1,2)=="35"|substr(provid,1,2)=="47"

	****Lags, cumulative counts, etc		
	foreach var in `varlist' {		
		foreach N in  270  {
			g `var'_`N'=0

		forvalues j=1/`N' {
			local i=`j'-1
		
			qui bysort prov (incidentdate): replace `var'_`N'=`var'_`N'+`var'[_n-`i']
		}	
		*i loop end
		}
		*N loop end
	}		
	*var loop end

	
	****Forward-looking	
	foreach var in `varlist' {		
		foreach N in   365 {
			g `var'_a`N'=0

		forvalues j=1/`N' {
			local i=`j'-1
		
			qui bysort prov (incidentdate): replace `var'_a`N'=`var'_a`N'+`var'[_n+`i']
		}	
		*i loop end
		}
		*N loop end
		rename `var' `var'_date
	}		
	*var loop end
	
	
	g day=day(incidentdate)
	g year=year(incidentdate)
	g month=month(incidentdate)
	save "bcms_byprov_allph_date", replace


*****Monthly Counts
use "bcms_edit", clear
	keep if uniqmun==1|uniqmun==2
	
	replace weight=1
	
	collapse (count) total=incidid (sum) $eventvarlist [iweight=weight] , by(munid year month)
	
	ds year month munid, not
	local varlist = r(varlist)
	  
	foreach var in `varlist' {
		rename `var' `var'_mun
	}


preserve
	keep munid 
	duplicates drop
	expand (2014-2011+1)*12
	bys munid: g year=2011+ceil(_n/12)-1
	bys munid year: g month=_n
	tempfile temp
	save `temp'
restore

merge 1:1 munid year month using `temp'
ds year month munid, not
local varlist=r(varlist)

foreach var in `varlist' {
	replace `var'=0 if _merge==2
}
drop _merge
sort mun year month
ds *_mun*
local varlist=r(varlist)
foreach var in `varlist' {
	bys munid (year month): g la_`var'=`var'[_n-1]
	bys munid (year month): g la2_`var'=`var'[_n-2]
	bys munid (year month): g la3_`var'=`var'[_n-3]
	bys munid (year month): g la4_`var'=`var'[_n-4]
	bys munid (year month): g la5_`var'=`var'[_n-5]
	bys munid (year month): g le_`var'=`var'[_n+1]
	bys munid (year month): g le2_`var'=`var'[_n+2]
	bys munid (year month): g le3_`var'=`var'[_n+3]
	bys munid (year month): g le4_`var'=`var'[_n+4]
	bys munid (year month): g le5_`var'=`var'[_n+5]
	
	
}	
save "bcms_bymun", replace

*Zero's for the rest of the Philippines and missing months: take list of municipalities/provinces from DHS
use "$dhs2013\phkr61dt\PHKR61FL", clear
	rename sprov provcode
	rename smunic municode
	rename sbarang barancode
	
	keep *code
	g provid=string(provcode,"%02.0f")
	g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
	g baranid=string(provcode,"%02.0f") +string(municode, "%02.0f") +string(barancode, "%03.0f")
	
	keep munid 
	duplicates drop
	expand (2014-2011+1)*12
	bys munid: g year=2011+ceil(_n/12)-1
	bys munid year: g month=_n
	merge 1:1 munid year month  using "bcms_bymun"
	
	ds year month munid _merge, not
	local varlist=r(varlist)
	
	g _inbcms=1 if _merge!=1
	bys munid: egen inbcms=max(_inbcms)
		*second line redundant	
	
	foreach var in `varlist' {
		replace `var'=0 if _merge==1
	}
	drop _*
	
	foreach var in `varlist' {
		bys munid (year month): g `var'_last2=`var'+`var'[_n-1] 
		bys munid (year month): g `var'_last3=`var'+`var'[_n-1] +`var'[_n-2]
	}
	
	g inarmm=inbcms
	replace inarmm=. if substr(munid,1,2)=="35"|substr(munid,1,2)=="47"
	save "bcms_bymun_allph", replace


*****DAILY COUNTS************************
*****************************************
use "bcms_edit", clear
keep if uniqmun==1|uniqmun==2
replace weight=1
collapse (count) total=incidid (sum) $eventvarlist [iweight=weight] , by(munid  incidentdate)

ds incidentdate munid, not
local varlist = r(varlist)

foreach var in `varlist' {
	rename `var' `var'_mun
}


preserve
keep munid 
duplicates drop
expand (365*4+1)
bys munid: g incidentdate=mdy(12,31,2010)+_n
format incidentdate %tdnn/dd/CCYY
tempfile temp
save `temp'
restore

merge 1:1 munid incidentdate using `temp'
ds incidentdate munid, not
local varlist=r(varlist)

foreach var in `varlist' {
replace `var'=0 if _merge==2
}
drop _merge

sort mun  incidentdate

save "bcms_bymun_date", replace


*Zero's for the rest of the Philippines and missing months: take list of municipalities/provinces from DHS
use "$dhs2013\phkr61dt\PHKR61FL", clear
rename sprov provcode
rename smunic municode
rename sbarang barancode

keep *code
g provid=string(provcode,"%02.0f")
g munid=string(provcode,"%02.0f") +string(municode, "%02.0f")
g baranid=string(provcode,"%02.0f") +string(municode, "%02.0f") +string(barancode, "%03.0f")

local byvar mun

keep munid 
duplicates drop
expand (365*4+1)
bys munid: g incidentdate=mdy(12,31,2010)+_n
format incidentdate %tdnn/dd/CCYY
merge 1:1 munid incidentdate using "bcms_bymun_date"

ds incidentdate _merge munid, not
local varlist=r(varlist)

foreach var in `varlist' {
	replace `var'=0 if _merge==1
}		

g _inbcms=1 if _merge!=1
bys munid: egen inbcms=max(_inbcms)
	*second line redundant


drop _*

g inarmm=inbcms
replace inarmm=. if substr(munid,1,2)=="35"|substr(munid,1,2)=="47"

****Lags, cumulative counts, etc		
foreach var in `varlist' {		
	foreach N in 270  {
		g `var'_`N'=0
		g la_`var'_`N'=0
		g la2_`var'_`N'=0
		g la3_`var'_`N'=0
		g la4_`var'_`N'=0
		g la5_`var'_`N'=0
		g le_`var'_`N'=0
		g le2_`var'_`N'=0
		g le3_`var'_`N'=0
		g le4_`var'_`N'=0
		g le5_`var'_`N'=0


	forvalues j=1/`N' {
		local i=`j'-1
	
		qui bysort mun (incidentdate): replace `var'_`N'=`var'_`N'+`var'[_n-`i']

		qui bysort mun (incidentdate): replace la_`var'_`N'=la_`var'_`N'+`var'[_n-`N'-`i']
		qui bysort mun (incidentdate): replace la2_`var'_`N'=la2_`var'_`N'+`var'[_n-`N'-`N'-`i']
		qui bysort mun (incidentdate): replace la3_`var'_`N'=la3_`var'_`N'+`var'[_n-`N'-`N'-`N'-`i']
		qui bysort mun (incidentdate): replace la4_`var'_`N'=la4_`var'_`N'+`var'[_n-`N'-`N'-`N'-`N'-`i']
		qui bysort mun (incidentdate): replace la5_`var'_`N'=la5_`var'_`N'+`var'[_n-`N'-`N'-`N'-`N'-`N'`i']
		
		qui bysort mun (incidentdate): replace le_`var'_`N'=le_`var'_`N'+`var'[_n+1+`i']
		qui bysort mun (incidentdate): replace le2_`var'_`N'=le2_`var'_`N'+`var'[_n+1+`N'+`i']
		qui bysort mun (incidentdate): replace le3_`var'_`N'=le3_`var'_`N'+`var'[_n+1+`N'+`N'+`i']
		qui bysort mun (incidentdate): replace le4_`var'_`N'=le4_`var'_`N'+`var'[_n+1+`N'+`N'+`N'+`i']
		qui bysort mun (incidentdate): replace le5_`var'_`N'=le5_`var'_`N'+`var'[_n+1+`N'+`N'+`N'+`N'+`i']
	}	
	*i loop end
	}
	*N loop end
}		
*var loop end


****Forward-looking	
foreach var in `varlist' {		
	foreach N in 365 {
		g `var'_a`N'=0
		g la_`var'_a`N'=0
		g la2_`var'_a`N'=0
		g la3_`var'_a`N'=0
		g la4_`var'_a`N'=0
		g la5_`var'_a`N'=0						
		g le_`var'_a`N'=0
		g le2_`var'_a`N'=0
		g le3_`var'_a`N'=0
		g le4_`var'_a`N'=0
		g le5_`var'_a`N'=0
		
	forvalues j=1/`N' {
		local i=`j'-1
	
		qui bysort mun (incidentdate): replace `var'_a`N'=`var'_a`N'+`var'[_n+`i']
		
		qui bysort mun (incidentdate): replace la_`var'_a`N'=la_`var'_a`N'+`var'[_n-1-`i']
		qui bysort mun (incidentdate): replace la2_`var'_a`N'=la2_`var'_a`N'+`var'[_n-1-`N'-`i']
		qui bysort mun (incidentdate): replace la3_`var'_a`N'=la3_`var'_a`N'+`var'[_n-1-`N'-`N'-`i']
		qui bysort mun (incidentdate): replace la4_`var'_a`N'=la4_`var'_a`N'+`var'[_n-1-`N'-`N'-`N'-`i']
		qui bysort mun (incidentdate): replace la5_`var'_a`N'=la5_`var'_a`N'+`var'[_n-1-`N'-`N'-`N'-`N'-`i']
		
		qui bysort mun (incidentdate): replace le_`var'_a`N'=le_`var'_a`N'+`var'[_n+`N'+`i']
		qui bysort mun (incidentdate): replace le2_`var'_a`N'=le2_`var'_a`N'+`var'[_n+`N'+`N'+`i']
		qui bysort mun (incidentdate): replace le3_`var'_a`N'=le3_`var'_a`N'+`var'[_n+`N'+`N'+`N'+`i']
		qui bysort mun (incidentdate): replace le4_`var'_a`N'=le4_`var'_a`N'+`var'[_n+`N'+`N'+`N'+`N'+`i']
		qui bysort mun (incidentdate): replace le5_`var'_a`N'=le5_`var'_a`N'+`var'[_n+`N'+`N'+`N'+`N'+`N'+`i']
		
	}	
	*i loop end
	}
	*N loop end
	rename `var' `var'_date
}		
*var loop end


g day=day(incidentdate)
g year=year(incidentdate)
g month=month(incidentdate)
save "bcms_bymun_allph_date", replace
		